<?php

namespace app\admin\controller;

use think\Controller;

class Config extends Controller
{
    //
    public function index()
    {
        $index    = db('config')->select();
        $viewData = [
            'index' => $index
        ];
        $this->assign($viewData);
        return view();
    }

    public function add()
    {
        if (request()->isPost()) {
            $data     = input('post.');
            $validate = validate('ConfValidate');
            if (!$validate->scene('add')->check($data)) {
                $this->error($validate->getError());
            }
            $add = db('config')->insert($data);
            if ($add) {
                $this->success('增加成功', url('lst'));
            } else {
                $this->error('失败');
            }
        }
        return view();
    }

    public function lst()
    {
        $confRes = db('config')->paginate(10);
        $this->assign('confRes', $confRes);
        return view();
    }

    public function del($id)
    {
        $del = db('config')->delete($id);
        if ($del) {
            $this->success('成功', url('lst'));
        } else {
            $this->error('失败');
        }
    }

    public function edit($id)
    {
        if (request()->isPost()) {
            $data     = input('post.');
            $validate = validate('ConfValidate');
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
            $save = db('config')->update($data);
            if ($save) {
                $this->success('编辑成功', url('lst'));
            } else {
                $this->error('编辑失败!');
            }
        }
        $confs    = db('config')->find($id);
        $viewData = [
            'confs' => $confs
        ];
        $this->assign($viewData);
        return view();
    }
}
